import 'dart:math';

import 'package:amap/amap.dart';
import 'package:flutter/material.dart';

class MarkerExtraData {
  int id;
  String name;

  MarkerExtraData({this.id, this.name});

  Map<String, dynamic> toJson() {
    return {
      'id': this.id,
      'name': this.name,
    };
  }

  factory MarkerExtraData.fromJson(Map<String, dynamic> map) {
    return new MarkerExtraData(
      id: map['id'] as int,
      name: map['name'] as String,
    );
  }
}

abstract class DummyData {
  static MarkerOptions createMarker(
    LatLng target, {
    String icon = "assets/icons/3.0x/location.png",
  }) {
    MarkerOptions option = MarkerOptions();
    option.position = target;
    option.title = "Hello, world";
    option.snippet = "HaHA";
    option.rotateAngle = pi / 4;
    option..infoWindowEnable = true;
    option.draggable = true;
    option.flat = false;
    option.icon = icon;
    option.extra = MarkerExtraData(id: 33, name: "James");
    return option;
  }

  static MarkerOptions createMarkerData(MapClickedEvent event) {
    return createMarker(event.target, icon: null);
  }

  static List<MarkerOptions> createMarkerListData(List<LatLng> points) {
    return points.map((p) => createMarker(p)).toList();
  }

  static CircleOptions createCircle(double latitude, double longitude) {
    final options = CircleOptions();
    options
      ..radius = 5.0
      ..fillColor = Colors.purple
      ..strokeColor = Colors.green
      ..strokeDottedLineType = MapConstants.DOTTED_LINE_TYPE_DEFAULT
      ..center = LatLng(latitude, longitude);
    return options;
  }

  static PolylineOptions createPolyline(List<LatLng> points, bool highlighted) {
    return PolylineOptions(
      points: points,
      color: Color(highlighted ? 0xff1890FF : 0xff13C2C2).value,
      width: 8,
      dottedLine: false,
      lineCapType: LineCapType.LineCapArrow,
      lineJoinType: LineJoinType.LineJoinRound,
    );
  }

  static List<LatLng> createPoints(LatLng target, int count) {
    return List.generate(count, (index) => target.random());
  }
}

extension LatLngExtentions on LatLng {
  LatLng random() {
    final random = Random();
    final deltaX = random.nextDouble() * 0.001 * (random.nextBool() ? 1 : -1);
    final deltaY = random.nextDouble() * 0.001 * (random.nextBool() ? 1 : -1);
    return LatLng(latitude + deltaX, longitude + deltaY);
  }
}
